//
//  TabbarViewController.m
//  MountEverestFM
//
//  Created by dllo on 15/10/30.
//  Copyright © 2015年 chenn. All rights reserved.
//

#import "TabbarViewController.h"
#import "MusicModel.h"
#import "PlayerModel.h"
#import "Player.h"
#import "HistoryModel.h"

@interface TabbarViewController ()

@end

@implementation TabbarViewController
@synthesize button;
@synthesize myTabbar;

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setUp];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -
-(void)setUp
{
   // 添加中间按钮
    [self addCenterButtonWithImage:[UIImage imageNamed:@"tabbar_np_normal@2x.png"] selectedImage:[UIImage imageNamed:@"tabbar_np_normal@2x.png"]];
    
    self.delegate = self;
    // 设定成中间页
    self.selectedIndex = 2;
    // 设置button状态
    button.selected = YES;
    
}

#pragma mark - 在中间的tabBar添加一个button
- (void) addCenterButtonWithImage:(UIImage *)buttonImage selectedImage:(UIImage *)selectedImage
{
    button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button addTarget:self action:@selector(pressChange:) forControlEvents:UIControlEventTouchUpInside];
    // 自动调整尺寸
    button.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
   
   
    // 设定button大小为适应图片
    button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
    [button setImage:buttonImage forState:UIControlStateNormal];
    [button setImage:selectedImage forState:UIControlStateSelected];
    [[NSUserDefaults standardUserDefaults]setObject:@"NO" forKey:@"isPlaying"];
    
    self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake( 3.5, 3.5, 40 , 40 )];
    _imageView.image = [UIImage imageNamed:@"toolbar_play_n_p@2x.png"];
    _imageView.backgroundColor = [UIColor clearColor];
    
    self.playImageView = [[UIImageView alloc]initWithFrame:CGRectMake(6.5, 6.5, 46, 46)];
    _playImageView.layer.masksToBounds = YES;
    _playImageView.layer.cornerRadius = 23;
    _imageView.backgroundColor = [UIColor clearColor];
    
    
    self.loopImageView = [[UIImageView alloc]initWithFrame:CGRectMake(3.5, 2, 59 , 59 )];
    _loopImageView.image = [UIImage imageNamed:@"tabbar_np_loop@2x.png"];
    
    [_loopImageView addSubview:_playImageView];
    
    [self addPlayImage:nil];
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(addPlayImage:) name:@"isPlaying" object:nil];
    
    // 去选中button时的阴影
    button.adjustsImageWhenHighlighted = NO;
    
    /*
     重点: 设置button的center 和 tabBar的center对齐, 同时做出此相对的悬浮
     */
    CGFloat heightDifference = buttonImage.size.height - self.tabBar.frame.size.height;
    if (heightDifference < 0) {
        button.center = self.tabBar.center;
    } else {
        CGPoint center = self.tabBar.center;
        center.y = center.y - heightDifference / 2.0;
        button.center = center;
    }
    
    [self.view addSubview:button];
}

- (void)addPlayImage:(NSNotification *)noti
{
    Player *player = [Player shareInstance];
    if (player.history == NO) {
        PlayerModel *playerModel = noti.object;
        MusicModel *musicModel = [playerModel.musicArray objectAtIndex:playerModel.index];
        NSURL *url = [NSURL URLWithString:musicModel.coverSmall];
        [_playImageView sd_setImageWithURL:url];
    } else {
        HistoryModel *historyModel = noti.object;
        NSURL *url = [NSURL URLWithString:historyModel.coverSmall];
        [_playImageView sd_setImageWithURL:url];
    }
    
    if ([[NSUserDefaults standardUserDefaults]boolForKey:@"isPlaying"]) {
//        创建动画
        [_imageView removeFromSuperview];
        CABasicAnimation *ani2 =  [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
        ani2.fromValue = @(0);
        ani2.toValue = @(2* M_PI);
        ani2.duration = 10;
        ani2.repeatCount = NSIntegerMax;
        [_loopImageView.layer addAnimation:ani2 forKey:@"loop"];
    } else {
        
        [_playImageView addSubview:_imageView];
        [_loopImageView.layer removeAnimationForKey:@"loop"];
    }
    
    
    [button addSubview:_loopImageView];
}

- (void)pressChange:(id)sender
{
    self.selectedIndex = 2;
    button.selected = YES;
}

#pragma mark - UITabBarController 协议方法
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    if (self.selectedIndex == 2) {
        button.selected = YES;
    } else {
        button.selected = NO;
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
